package com.example.demo.async.config;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 初始化多个线程池，用于隔离@Async异步任务
 *
 */
@EnableAsync
@Configuration
public class TaskPoolConfig {

//    @Bean
//    public ThreadPoolExecutor threadPoolExecutor() {
//        return new ThreadPoolExecutor(
//                5,
//                10,
//                60000,
//                TimeUnit.MILLISECONDS,
//                new ArrayBlockingQueue<Runnable>(200),
//                new ThreadFactory() {
//                    private final AtomicInteger threadNumber = new AtomicInteger(1);
//                    private final ThreadGroup group = new ThreadGroup("app");
//
//                    @Override
//                    public Thread newThread(Runnable r) {
//                        return new Thread(group, r,"thread-" + threadNumber.getAndIncrement(), 0);
//                    }
//
//                });
//    }
	
    @Bean
    public Executor taskExecutor1() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(10);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("executor-1-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
//        //配置拒绝策略
//        // AbortPolicy策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
//        // DiscardPolicy策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
//        // DiscardOldestPolicy策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
//        // CallerRunsPolicy策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        //自定义拒绝策略1
//        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
//            @Override
//            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//                // 拒绝策略的逻辑
//            }
//        });
//        //自定义拒绝策略2
//        executor.setRejectedExecutionHandler((r, executor1) -> {
//            // 拒绝策略的逻辑
//        });
        
        return executor;
    }

    @Bean
    public Executor taskExecutor2() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(10);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("executor-2-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}